package com.zuipin.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.IntegralRecord;
import com.zuipin.entity.OrderInfo;
import com.zuipin.entity.SysUser;
import com.zuipin.enums.IntegralType;
import com.zuipin.enums.RecordSearchTime;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.mapper.OrderInfoMapper;
import com.zuipin.service.IIntegralRecordService;
import com.zuipin.service.ISysUserService;
import com.zuipin.util.Pagination;
import com.zuipin.util.RegexUtils;
import com.zuipin.vo.IntegralAndRecordVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/** 
 * @ClassName: IntegralRecordServiceImpl 
 * @author wuyicheng
 * @time 2017年3月27日下午4:03:17 
 */
@RestController
@Api(value = "积分变更记录", description = "积分变更记录-wuyicheng")
@RequestMapping(value = "/member/integralRecord")
public class IntegralRecordController {
	
	private static Logger			log	= LoggerFactory.getLogger(IntegralRecordController.class);
	@Resource
	private IIntegralRecordService	integralRecordService;
	
	@Resource
	private OrderInfoMapper			orderInfoMapper;
	
	@Resource
	private ISysUserService			sysUserService;
	
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	@ApiOperation(value = "查询积分变更记录", notes = "查询积分变更记录")
	@Valids({ @Valid(name = "memberId", minValue = 1), @Valid(name = "sysId", minValue = 1),
			@Valid(name = "searchTime", required = true, regex = "THREE_MONTH_AGO|ONE_YEAR_AGO|ALL_AGO") })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "integralType", value = "'REGISTER','SIGN_IN','GET_SHOPPING','USE_SHOPPING','ORDER_RECYCLING','OTHERS'类型：会员注册送积分  会员签到  购物赠送积分 购物积分抵扣  回收赠送积分 其他", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "searchTime", value = "THREE_MONTH_AGO（三个月前）|ONE_YEAR_AGO（一年内）|ALL_AGO（全部时间）", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query")//
	})
	public PageResult<IntegralAndRecordVo> search(@RequestParam Long memberId, @RequestParam Long sysId, @RequestParam(required = false) IntegralType integralType,
			@RequestParam RecordSearchTime searchTime, @Inject @ApiIgnore Pagination page) {
		
		// 当前积分和总记录
		PageResult<IntegralAndRecordVo> result = new PageResult<>();
		
		IntegralAndRecordVo integralAndRecordVo = new IntegralAndRecordVo();
		IntegralRecord integralRecord = new IntegralRecord();
		
		integralRecord.setSysId(sysId);
		integralRecord.setMemberId(memberId);
		integralRecord.setSearchTime(searchTime.getKey());
		integralRecord.setIntegralType(integralType);
		
		// 查询出当前积分
		Integer currentIntegral = integralRecordService.getCurrentIntegral(memberId, sysId);
		integralAndRecordVo.setIntegral(currentIntegral == null ? 0 : currentIntegral);
		
		// 积分记录
		List<IntegralRecord> list = integralRecordService.findListByPage(integralRecord, page);
		
		if (list != null && list.size() > 0) {
			// 填充订单ID给前台
			for (IntegralRecord ir : list) {
				
				SysUser user = sysUserService.findById(ir.getUpdateId());
				if (user != null) {
					ir.setUpdateName(user.getRealName());
				}
				
				OrderInfo orderInfo = orderInfoMapper.findOrderInfoByOrderNo(ir.getOddNumbers());
				if (orderInfo != null) {
					ir.setOrderId(orderInfo.getId());
				}
			}
			
			integralAndRecordVo.setRecords(list);
			result.setTotal_record(list.size());
			integralAndRecordVo.setRecords(list);
			result.setData(integralAndRecordVo);
		} else {
			result.setData(integralAndRecordVo);
			result.setTotal_record(0);
		}
		
		result.setTotal_record(page.getTotalRecord());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/addIntegralRecord", method = RequestMethod.POST)
	@ApiOperation(value = "积分变更接口-wuyicheng 备注：购物送积分（订单号：123456)  remark保存着购物送积分字段  describes保存 订单号字段  oddNumbers保存单号字段", notes = "积分变更接口-wuyicheng 备注：购物送积分（订单号：123456)  remark保存着购物送积分字段  describes保存 订单号字段  oddNumbers保存单号字段")
	@Valids({ @Valid(name = "memberId", minValue = 1), @Valid(name = "sysId", minValue = 1), @Valid(name = "alterPoint", regex = RegexUtils.INTEGER),
			@Valid(name = "integralType", regex = "PAY|GIFT_CARD|REFUND|BUY_USE"), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "createId", value = "创建人ID", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "updateId", value = "修改人ID", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "alterPoint", value = "修改的积分", required = true, dataType = "bigDecimal", paramType = "query"),
			@ApiImplicitParam(name = "integralType", value = "'REGISTER','SIGN_IN','GET_SHOPPING','USE_SHOPPING','ORDER_RECYCLING','OTHERS'类型：会员注册送积分  会员签到  购物赠送积分 购物积分抵扣  回收赠送积分 其他", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "remark", value = "备注", required = false, dataType = "string", paramType = "query"), })
	
	public Result<Map<String, Object>> addIntegralRecord(@RequestParam Long memberId, @RequestParam Long sysId, @RequestParam(required = false) Long createId,
			@RequestParam(required = false) Long updateId, @RequestParam Integer alterPoint, @RequestParam IntegralType integralType, @RequestParam(required = false) String remark)
			throws CustomException {
		
		Result<Map<String, Object>> result = new Result<>();
		integralRecordService.addIntegralRecord(memberId, sysId, createId, updateId, alterPoint, integralType, remark, "", "", IntegralRecord.IS_BUY_NO);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setData(null);
		return result;
	}
	
}
